package 笔试.笔试360;

import java.util.Scanner;

/**
 * @author:谢君臣
 * @Date:2021/9/25-15:59
 * @version:1.0
 * @function:
 */
public class Main01 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int [] arr = new int[n];
        int i =0;
        while (i<n)arr[i++] = scanner.nextInt();
        int a=1,b=n-2,count=0;
        while (a<b){
            while (a<b&&arr[a-1]>arr[a])a++;
            while (a<b&&arr[b]<arr[b+1])b--;
            if (b>a){
                if (b-a>1){
                    int temp1 = arr[a];
                    arr[a] = arr[a-1]-1;
                    int temp2 = arr[b];
                    arr[b] = arr[b+1]-1;
                    count = count + temp1-arr[a] + temp2 -arr[b];
                }
                else {  //当前只剩a\b
                    count +=Math.min(arr[a]-arr[a-1]+1,arr[b]-arr[b+1]+1);
                    count++;
                    break;
                }
            }
        }
        System.out.println(count);
    }
}
